<template>
  <div>
    <el-table
      ref="multipleTable"
      :data="typelist"
      tooltip-effect="dark"
      @selection-change="handleSelectionChange">
      <el-table-column
        align="center"
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        align="center"
        prop="id"
        label="ID"
        width="250">
      </el-table-column>
      <el-table-column
        align="center"
        prop="sort"
        label="排序"
        width="250">
      </el-table-column>
      <el-table-column
        align="center"
        prop="typename"
        label="栏目名称"
        width="500">
      </el-table-column>
      <el-table-column width="300" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-button @click="batchDel()">批量删除</el-button>
      <el-button @click="toggleSelection()">取消选择</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'type-list',
  data () {
    return {
      typelist: [],
      multipleSelection: []
    }
  },
  methods: {
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    // 批量删除
    batchDel () {
      if (this.multipleSelection) {
        let ids = this.multipleSelection.map(item => item.id).join()
        if (ids.length > 0) {
          this.$http.get('/news/deleteNewsType', {
            params: {
              ids: ids
            }
          }).then(res => {
            let data = res.data
            let status = data.status
            if (status === 200) {
              this.getTypeList()
            }
          })
        }
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    getTypeList () {
      this.$http.get('/news/getNewsTypeList')
        .then(this.getTypeListInfoSucc)
    },
    getTypeListInfoSucc (res) {
      let data = res.data
      if (data.status === 200) {
        this.typelist = data.data
      }
    },
    handleDelete (index, row) {
      this.$http.get('/news/deleteNewsType', {
        params: {
          ids: row.id
        }
      }).then(res => {
        let data = res.data
        let status = data.status
        if (status === 200) {
          this.getTypeList()
        }
      })
    },
    handleEdit (index, row) {
      this.$router.push({
        name: 'EditNewsType',
        params: {
          id: row.id,
          typename: row.typename,
          sort: row.sort
        }
      })
    }
  },
  mounted () {
    this.getTypeList()
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">

</style>
